1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <sys/socket.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <assert.h> #include <string.h>
static const int CONTROL_LEN = CMSG_LEN(sizeof(int));
void send_fd(int fd, int fd_to_send) { struct iovec iov[1]; struct msghdr msg; char buf[0];
iov[0].iov_base = buf; iov[0].iov_len = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = iov; msg.msg_iovlen = 1;
cmsghdr cm; cm.cmsg_len = CONTROL_LEN; cm.cmsg_level = SOL_SOCKET; cm.cmsg_type = SCM_RIGHTS; *(int *)CMSG_DATA(&cm) = fd_to_send; msg.msg_control = &cm; msg.msg_controllen = CONTROL_LEN;
sendmsg(fd, &msg, 0); }
int recv_fd(int fd) { struct iovec iov[1]; struct msghdr msg; char buf[0];
iov[0].iov_base = buf; iov[0].iov_len = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = iov; msg.msg_iovlen = 1;
cmsghdr cm; msg.msg_control = &cm; msg.msg_controllen = CONTROL_LEN;
recvmsg(fd, &msg, 0);
int fd_to_read = *(int *)CMSG_DATA(&cm); return fd_to_read; }
int main() { int pipefd[2]; int fd_to_pass = 0;
int ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, pipefd); assert(ret != -1);
pid_t pid = fork(); assert(pid >= 0);
if (pid == 0) { close(pipefd[0]); fd_to_pass = open("test.txt", O_RDWR, 0666); send_fd(pipefd[1], (fd_to_pass > 0) ? fd_to_pass : 0); close(fd_to_pass); exit(0); }
close(pipefd[1]); fd_to_pass = recv_fd(pipefd[0]); char buf[1024]; memset(buf, '\0', 1024); read(fd_to_pass, buf, 1024); printf("I got fd %d and data %s\n", fd_to_pass, buf); close(fd_to_pass); }
|